<template>
  <base-breadcrumb>
    <a-card class="mb20">
      <titleName
        ref="box"
        title="会议发起"
      />
      <a-form-model
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 16 }"
      >
        <a-row :gutter="24">
          <a-col
            v-if="!meetingType"
            :md="24"
            :sm="24"
          >
            <a-form-model-item label="会议类型：">
              <a-radio-group
                v-model="form.value"
                class="mb20"
                :options="meetingTypeList"
                @change="changeValue"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
              <a-form-model-item
                label="所属单位："
                prop="projectId"
              >
                <org-tree-select v-model="form.projectId" is-need-auth @select="selectOrg" check-strictly></org-tree-select>
              </a-form-model-item>
          </a-col>
          <a-col
            v-if="form.value == 4"
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="月份："
              prop="month"
            >
              <a-month-picker
                v-model="form.month"
                placeholder="请输入"
                @change="(date, dateString) => getTime(date, dateString, 'form', 'month')"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            v-if="form.value == 7"
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="交底方案："
              prop="commentedPlan"
            >
              <a-select
                v-model="form.commentedPlan"
                placeholder="请选择"
                @change="changeCommentedPlan"
              >
                <a-select-option
                  v-for="item in disclosurePlan"
                  :key="item.id"
                  :value="item.id"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
            class="requiredFormItem"
          >
            <a-form-model-item
              label="监理会议期数："
              prop="meetingName"
            >
              <a-input-number
                v-model="form.meetingName"
                placeholder="请输入"
                :precision='0'
                :min='1'
                @change="getMeetingList"
              />
              <!-- <BaseInput v-model.trim="form.meetingName" :max-length="30" placeholder="请输入" type="number" /> -->
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="是否具备召开会议条件"
              :label-col="{ lg: { span:5 }, sm: { span: 5 } }"
              :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              prop="isHaveMeetCondition"
            >
              <a-select
                v-model="form.isHaveMeetCondition"
                placeholder="请选择"
              >
                <a-select-option :value="1" >是</a-select-option>
                <a-select-option :value="0" >否</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col
            :md="24"
            :sm="24"
            v-if="form.isHaveMeetCondition === 0"
          >
            <a-form-model-item
              label="未召开会议原因："
              prop="notMeetReason"
            >
              <BaseTextarea
                v-model.trim="form.notMeetReason"
                :max-length="300"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
            v-if="form.isHaveMeetCondition === 1"
          >
            <a-form-model-item
              label="主持人/主讲人："
              prop="speaker"
            >
              <!-- <BaseInput :maxLength="30" placeholder="请输入" v-model.trim="form.speaker" /> -->
              <SelectPersonByProject
                :id="form.branchCompanyId"
                v-model="form.speaker"
                :project-id="form.projectId"
                :show-search="true"
                mode="combobox"
                placeholder="请选择"
                option-label-prop="children"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
            v-if="form.isHaveMeetCondition === 1"
          >
            <a-form-model-item
              label="会议地点："
              prop="meetingPlace"
            >
              <BaseInput
                v-model.trim="form.meetingPlace"
                :max-length="300"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>

          <a-col
            :md="8"
            :sm="8"
            v-if="form.isHaveMeetCondition === 1"
          >
            <a-form-model-item
              label="会议时间："
              prop="startTime"
            >
              <a-date-picker
                v-model="form.startTime"
                :show-time="{ format: 'HH:mm' }"
                format="YYYY-MM-DD HH:mm"
                placeholder="请输入"
                @change="(date, dateString) => getTime(date, dateString, 'form', 'startTime')"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
            v-if="form.isHaveMeetCondition === 1"
          >
            <a-form-model-item label="记录人：">
              <BaseInput
                v-model="creatorName"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
            v-if="form.isHaveMeetCondition === 1"
          >
            <a-form-model-item label="记录时间：">
              <BaseInput
                v-model="createTime"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="24"
            :sm="24"
            v-if="form.isHaveMeetCondition === 1"
          >
            <a-form-model-item
              label="会议说明："
              prop="meetingExplains"
            >
              <BaseTextarea
                v-model.trim="form.meetingExplains"
                :max-length="300"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <div v-if="form.isHaveMeetCondition === 1">
      <!-- 施工方案评审会议 -->
      <a-form-model
        v-show="form.value == '1'"
        ref="ruleForm1"
        :model="form"
        :rules="rulesType1"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 16 }"
      >
        <a-row :gutter="24">
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="评审方案："
              prop="commentedPlan"
            >
              <a-select
                v-model="form.commentedPlan"
                placeholder="请选择"
                @change="changeReviewPlan"
              >
                <a-select-option
                  v-for="item in reviewPlan"
                  :key="item.id"
                  :value="item.id"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>

          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="方案编制人"
              prop="programmer"
            >
              <BaseInput
                v-model.trim="form.programmer"
                disabled
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="24"
            :sm="24"
          >
            <a-form-model-item
              label="其他评审人员："
              prop="otherReviewer"
            >
              <BaseTextarea
                v-model.trim="form.otherReviewer"
                :max-length="300"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <!-- 专业分包方案讲评 -->
      <a-form-model
        v-show="form.value == '2'"
        ref="ruleForm2"
        :model="form"
        :rules="rulesType2"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 16 }"
      >
        <a-row :gutter="24">
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="评讲方案："
              prop="commentedPlan"
            >
              <a-select
                v-model="form.commentedPlan"
                placeholder="请选择"
                @change="changeCommentedPlan"
              >
                <a-select-option
                  v-for="item in constructionProgram"
                  :key="item.id"
                  :value="item.id"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="方案编制人："
              prop="programmer"
            >
              <BaseInput
                v-model.trim="form.programmer"
                disabled
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="专业单位名称："
              prop="professionComName"
            >
              <SelectSubcontractor
                v-model="form.professionComName"
                placeholder="请选择"
                :form="form"
                :is-clear="id == ''"
                key-value="projectId"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <!-- 危险源识别清单发布 -->
      <a-form-model
        v-show="form.value == '3'"
        ref="ruleForm3"
        :model="form"
        :rules="rulesType3"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 16 }"
      >
        <a-row :gutter="24">
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="会议月份："
              prop="hsemonth"
            >
              <a-month-picker
                v-model="form.hsemonth"
                placeholder="请输入"
                @change="(date, dateString) => getTime(date, dateString, 'form', 'hsemonth')"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="当前施工阶段："
              prop="currentConstructionStage"
            >
              <BaseInput
                v-model.trim="form.currentConstructionStage"
                :max-length="30"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <!-- 安全教育记录 -->
      <a-form-model
        v-show="form.value == '5'"
        ref="ruleForm5"
        :model="form"
        :rules="rulesType5"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 16 }"
      >
        <a-row :gutter="24">
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="教育课时："
              prop="educationHour"
            >
              <BaseInput
                v-model.trim="form.educationHour"
                :max-length="30"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="教育负责人："
              prop="educationResponsible"
            >
              <SelectPersonByProject
                :id="form.branchCompanyId"
                v-model="form.educationResponsible"
                :project-id="form.projectId"
                :show-search="true"
              />
              <!-- <OrgStaffAsync
                  placeholder="请选择"
                  v-model="form.educationResponsible"
                  :disableCheckbox="true"
                  :orgCheckable="true"
                  :selectedValue.sync="form.educationResponsible"
                  :treeCheckable="false"
              /> -->
              <!-- <BaseInput placeholder="请输入" v-model.trim="form.educationResponsible" /> -->
            </a-form-model-item>
          </a-col>
          <a-col
            :md="24"
            :sm="24"
          >
            <a-form-model-item
              label="受教育单位："
              prop="educationOrganization"
            >
              <BaseTextarea
                v-model.trim="form.educationOrganization"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <!-- 施工方案交底记录 -->
      <a-form-model
        v-show="form.value == '7'"
        ref="ruleForm7"
        :model="form"
        :rules="rulesType7"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 16 }"
      >
        <!-- <a-row :gutter="24">
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="参会人数："
              prop="meetingCount"
            >
              <BaseInputNumber
                v-model.trim="form.meetingCount"
                :min="0"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="24"
            :sm="24"
          >
            <a-form-model-item
              label="主要参会人员："
              prop="mainParticipants"
            >
              <BaseTextarea
                v-model.trim="form.mainParticipants"
                :max-length="300"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
        </a-row> -->
      </a-form-model>
      <!-- 监理例会 设计例会 -->
      <a-form-model
        v-show="form.value == '8' || form.value == '9'"
        ref="ruleForm8"
        :model="form"
        :rules="rulesType8"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 16 }"
      >
        <a-row :gutter="24">
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="参会人数："
              prop="meetingCount"
            >
              <BaseInputNumber
                v-model.trim="form.meetingCount"
                :min="0"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="24"
            :sm="24"
          >
            <a-form-model-item
              label="主要参会人员："
              prop="mainParticipants"
            >
              <BaseTextarea
                v-model.trim="form.mainParticipants"
                :max-length="300"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <!-- <a-card class="mb20"> -->
      <a-form-model
        :model="form"
        :rules="rulesType9"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 16 }"
      >
        <div v-if="form.value == '8' || form.value == '9'">
          <a-row :gutter="24">
            <a-col span="24">
              <a-form-model-item
                label="会议纪要"
                prop="meetingSummary"
              >
                <!-- <editor
                    v-model.trim="form.meetingSummary"
                    :isClear="true"
                     :maxLength="30000"
                    @change="commentsVal"
                ></editor> -->
                <BaseTextarea
                  v-model="form.meetingSummary"
                  :auto-size="{ minRows: 20, maxRows: 25 }"
                  :max-length="30000"
                  placeholder="请输入"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
        </div>
        <div v-else>
          <a-row :gutter="24">
            <a-col
              :md="24"
              :sm="24"
            >
              <a-form-model-item
                label="参会者选择："
                prop="meetingEmployeeDTOList"
              >
                <!-- <OrgStaffAsync
                    placeholder="请选择/多选"
                    v-model="form.meetingEmployeeDTOList"

                    :orgCheckable="true"
                    :selectedValue.sync="form.meetingEmployeeDTOList"
                    :treeCheckable="true"
                /> -->

                <!-- <a-col :span="20"> -->
                <!-- 操作栏 -->
                <div class="mb12">
                  <!-- <a-button
                    icon="plus"
                    class="btn_right"
                    type="primary"
                    @click="visible = true"
                  >
                    添加人员
                  </a-button> -->
                  <a-button
                    icon="plus"
                    class="btn_right"
                    type="primary"
                    @click="openSelectPersonModal"
                  >
                    添加人员
                  </a-button>
                  <a-button
                    :disabled="rowSelection.selectedRowKeys.length < 1"
                    icon="delete"
                    type="danger"
                    @click="handleRemoveMore"
                  >
                    移除
                  </a-button>
                </div>
                <!-- 统计选中项 -->
                <a-alert :show-icon="true">
                  <template slot="message">
                    <span style="margin-right: 12px">
                      已选择:
                      <a>
                        <b>{{ rowSelection.selectedRowKeys.length }}</b>
                      </a>
                      项
                    </span>
                  </template>
                </a-alert>
                <!-- 表格 -->
                <s-table
                  ref="tableEmployee"
                  table-layout="fixed"
                  size="default"
                  row-key="id"
                  :columns="columnsEmployee"
                  :data="loadDataEmployee"
                  :alert="true"
                  :row-selection="rowSelection"
                  show-size-changer
                  show-quick-jumper
                  show-pagination="auto"
                >
                  <!-- 表格行操作栏 -->
                  <span
                    slot="actions"
                    slot-scope="text, record, index"
                  >
                    <template>
                      <!-- <a @click="handleManage(record)">查看</a>
                                        <a-divider type="vertical" /> -->
                      <a @click="handleRemove(index)">移除</a>
                    </template>
                  </span>
                </s-table>
              </a-form-model-item>
              <!-- </a-col> -->
            </a-col>
          </a-row>
        </div>
      </a-form-model>
      <!-- </a-card> -->
      <template v-if="form.value == '8' || form.value == '9'">
        <titleName
          title="相关资料上传"
          class="title-style"
        />

        <a-form-model
          :model="form"
          :label-col="{ span: 6 }"
          :wrapper-col="{ span: 16 }"
          ref="ruleForm88"
          :rules="rulesType88"
          class="ruleForm"
        >
          <a-row :gutter="24">
            <a-col span="24">
              <a-form-model-item
                label="会议纪要"
                prop="summaryFileList"
                class="tip-red"
              >
                <l-table
                  text="上传附件"
                  btn-type="primary"
                  :upload-arrys.sync="form.summaryFileList"
                  business-code="SHE04"
                  :needChineseName="true"
                  errorTips="附件名称中必须含有中文"
                />
              </a-form-model-item>
            </a-col>
            <a-col span="24">
              <a-form-model-item
                label="会议纪要整改回复"
                prop="summaryReplyFileList"
                class="tip-red"
              >
                <l-table
                  text="上传附件"
                  btn-type="primary"
                  :upload-arrys.sync="form.summaryReplyFileList"
                  business-code="SHE04"
                  :needChineseName="true"
                  errorTips="附件名称中必须含有中文"
                />
              </a-form-model-item>
            </a-col>
            <a-col span="24">
              <a-form-model-item
                label="其他附件"
                prop="fileList"
              >
                <l-table
                  text="上传附件"
                  btn-type="primary"
                  :upload-arrys.sync="form.fileList"
                  business-code="SHE04"
                  :needChineseName="true"
                  errorTips="附件名称中必须含有中文"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </template>
      </div>
    </a-card>

    <div class="tool-btns page-btn-right-top">
      <a-button
        class="ml20"
        :loading="loading"
        type="primary"
        @click="addMeeting"
        :disabled="meetingNamevalidatorFlag"
      > 提交 </a-button>
      <a-button
        class="ml20"
        type="primary"
        @click="handleClose"
      > 关闭 </a-button>
    </div>

    <!-- 表单弹窗 -->
    <dialog-edit
      ref="dialogEdit"
      :meeting-employee-d-t-o-list-data="form.meetingEmployeeDTOListData"
      :visible.sync="visible"
      @confirm="submitForm"
      @confirms="submitForms"
    />
    <SelectPerson
      ref="SelectPersonWithTree"
      headerTitle="添加参会人员"
      :unRepeatPerson="true"
      @confirm="submitForms"
    />
  </base-breadcrumb>
</template>
<script>
const columnsEmployee = [
  {
    title: '员工名称',
    dataIndex: 'name'
  },

  {
    title: '电话号码',
    dataIndex: 'phone'
  },

  {
    title: '操作',
    dataIndex: 'actions',
    width: '100px',
    scopedSlots: { customRender: 'actions' }
  }
]
const columns = [
  {
    title: '序号',
    dataIndex: 'serial',
    width: '5%',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '文件名称',
    dataIndex: 'fileName',
    width: '40%'
  },
  {
    title: '文件格式',
    dataIndex: 'suffixName'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '10%',
    scopedSlots: { customRender: 'action' }
  }
]

import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectPersonByProject from '@/components/Select/SelectPersonByProject' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 分公司
import SelectSubcontractor from '@/components/Select/SelectSubcontractor' // 分包商
import { STable } from '@/components'
import dialogEdit from './dialogEdit.vue' // 表单弹窗
import { getProjectInfo } from '@/api/businessAffairs/revenueAndCost'
import SelectPerson from '@/components/BComponents/BSelectPerson'
import { getMeetingList } from '@/api/office/conference'
import moment from 'moment'
import { getMeetingType, addMeeting, getConstructionProgram, getReviewPlan, getDisclosurePlan } from '@/api/office/conference'
import Vue from 'vue'
import _ from 'lodash'

export default {
  name: 'supervisionMeetingUpdate',
  components: {
    SelectSubCompany,
    SelectProject,
    STable,
    SelectSubcontractor,
    SelectPersonByProject,
    dialogEdit,
    SelectPerson
  },
  data() {
    // 验证大于0
    let validateGt0 = (rule, value, callback) => {
      if (value === '') {
        callback('请输入监理会议期数')
      } else {
        if (Number(value) <= 0) {
          callback('监理会议期数不能小于0')
        }
        callback()
      }
    }
    return {
      columns,
      columnsEmployee,
      meetingType: false,
      listing: false, // 防止getMeetingList 多次请求
      meetingNamevalidator: false, // 所属项目该期数已存在
      meetingNamevalidatorFlag: false, // 所属项目该期数已存在隐藏按钮
      token: Vue.getAcloudProvider().getAccessToken(),
      createTime: moment().format('YYYY-MM-DD'),
      creatorName: JSON.parse(sessionStorage.getItem('ycloud-user_info')).name,
      form: {
        educationHour: '',
        educationResponsible: '',
        educationOrganization: '',
        value: 1,
        meetingSummary: '',
        fileList: [],
        summaryFileList: [],
        summaryReplyFileList: [],
        meetingEmployeeDTOList: [],
        meetingEmployeeDTOListData: [],
        branchComId: '',
        projectId: '',
        name: '',
        meetingName: '',
        isHaveMeetCondition: 1,
        notMeetReason: '',
        startTime: '',
        meetingExplains: '',
        commentedPlan: '',
        programmer: '',
        otherReviewer: '',
        professionComName: '',
        hsemonth: '',
        currentConstructionStage: '',
        meetingCount: '',
        mainParticipants: ''
      },
      rulesType: {
        branchComId: [{ required: true, message: '请选择分公司', trigger: 'blur' }],
        projectId: [{ required: true, message: '请选择项目' }],
        name: [{ required: true, message: '请输入名称' }]
      },
      rules: {
        month: [{ required: true, message: '请选择月份' }],
        commentedPlan: [{ required: true, message: '请选择交底方案', trigger: 'blur' }],
        branchComId: [{ required: true, message: '请选择分公司', trigger: 'blur' }],
        projectId: [{ required: true, message: '请选择项目', trigger: 'blur' }],
        meetingName: [
          {
            validator: validateGt0,
            trigger: 'change'
          }
        ],
        isHaveMeetCondition: [{ required: true, message: '是否具备召开会议条件', trigger: 'blur' }],
        notMeetReason: [{ required: true, message: '请输入未召开会议原因', trigger: 'blur' }],
        speaker: [{ required: true, message: '请输入主持人/主讲人', trigger: 'blur' }],
        meetingPlace: [{ required: true, message: '请输入会议地点', trigger: 'blur' }],
        startTime: [{ required: true, message: '请输入会议时间', trigger: 'change' }],
        meetingExplains: [{ required: true, message: '请输入会议说明', trigger: 'blur' }]
      },
      rulesType1: {
        commentedPlan: [{ required: true, message: '请选择评审方案', trigger: 'blur' }],
        programmer: [{ required: true, message: '请选择方案编制人', trigger: 'blur' }],
        otherReviewer: [{ required: true, message: '请选择其他评审人员', trigger: 'blur' }]
      },
      rulesType2: {
        commentedPlan: [{ required: true, message: '请选择讲评方案', trigger: 'blur' }],
        programmer: [{ required: true, message: '请选择方案编制人', trigger: 'blur' }],
        professionComName: [{ required: true, message: '请选择专业单位名称', trigger: 'blur' }]
      },
      rulesType3: {
        hsemonth: [{ required: true, message: '请输入月份', trigger: 'change' }],
        currentConstructionStage: [{ required: true, message: '请输入当期施工阶段', trigger: 'blur' }]
      },
      rulesType5: {
        educationHour: [{ required: true, message: '请输入教育课时', trigger: 'blur' }],
        educationResponsible: [{ required: true, message: '请选择教育负责人', trigger: 'change' }],
        educationOrganization: [{ required: true, message: '请输入受教育单位', trigger: 'blur' }]
      },

      rulesType7: {
        commentedPlan: [{ required: true, message: '请选择交底方案', trigger: 'blur' }],
        // disclosurePlan: [{ required: true, message: '请输入交底方案', trigger: 'blur' }],
        meetingCount: [{ required: true, message: '请输入参会人数', trigger: 'blur' }],
        mainParticipants: [{ required: true, message: '请输入主要参会人员', trigger: 'blur' }]
      },

      rulesType8: {
        meetingCount: [{ required: true, message: '请输入参会人数', trigger: 'blur' }],
        mainParticipants: [{ required: true, message: '请输入主要参会人员', trigger: 'blur' }]
      },
      rulesType9: {
        meetingSummary: [{ required: true, message: '请输入会议纪要', trigger: 'blur' }],
      },

      rulesType88: {
        summaryFileList: [{ type: 'array', required: true, message: '请上传会议纪要文件', trigger: 'blur' }],
        summaryReplyFileList: [{ type: 'array', required: true, message: '请上传会议纪要整改回复文件', trigger: 'blur' }]
      },

      loadData: () => {
        return new Promise((resolve, reject) => {
          resolve({
            data: this.form.fileList
          })
        })
      },
      loadDataEmployee: (parameter) => {
        return new Promise((resolve, reject) => {
          let obj = {}
          obj.currentPage = parameter.pageNo
          obj.pageSize = parameter.pageSize
          obj.totalPage = Math.ceil(this.form.meetingEmployeeDTOListData.length / parameter.pageSize)
          obj.totalSize = this.form.meetingEmployeeDTOListData.length
          obj.data = this.form.meetingEmployeeDTOListData || []
          console.log(obj)
          resolve(obj)
        })
      },
      loading: false,
      id: '',
      isUpdate: true,
      meetingTypeList: [], //会议类别
      constructionProgram: [],
      reviewPlan: [],
      disclosurePlan: [],
      visible: false,
      // 表格复选框配置
      rowSelection: {
        selectedRowKeys: [],
        onChange(selectedRowKeys) {
          this.selectedRowKeys = selectedRowKeys
        }
      },
      selectedKeys: [], // 选中节点
      selectOptions: {}, // 选中节点对象
      projectDeptOrgId: '', //项目所在的树节点Id
    }
  },
  mounted() {
    this.getMeetingType()
    this.meetingType = false
    if (this.$route.query.typeId) {
      this.form.value = this.$route.query.typeId
      this.meetingType = true
      this.initTitle()
    }
  },

  watch: {
    form: {
      handler(val) {
        const { summaryFileList, summaryReplyFileList } = val
        if (summaryFileList && summaryFileList.length) {
          this.$refs['ruleForm88'].validateField('summaryFileList')
        }
        if (summaryReplyFileList && summaryReplyFileList.length) {
          this.$refs['ruleForm88'].validateField('summaryReplyFileList')
        }
      },
      deep: true
    }
  },

  methods: {
    selectOrg(val) {
      if(val&&val.length) {
          const projectId = val[val.length - 1].projectId
          this.form.projectId = projectId;
          this.form.branchComId = val[val.length - 2].orgId;
          this.$refs.ruleForm.clearValidate('projectId')
          this.getConstructionProgram(projectId) //获取专业分包
          this.getReviewPlan(projectId) //获取评审方案
          this.getDisclosurePlanReq(projectId)
          this.getProjectInfo(projectId)
      }
    },
    moment,
    // parserHandler(text = '') {
    //   if (text !== '') {
    //     if (/^\d+$/.test(text)) {
    //       return text
    //     } else {
    //       return 1
    //     }
    //   } else {
    //     return text
    //   }
    // },
    // async getMeetingList() {
    //   if (this.form.meetingName && !this.listing) {
    //       this.listing = true
    //       const [err, res] = await getMeetingList({
    //         pageNo: 1,
    //         pageSize: 100,
    //         query: {
    //           meetingName: `监理例会会议纪要-${this.form.meetingName}期`,
    //           projectId: this.form.projectId
    //         }
    //       })
    //       this.listing = false
    //       try {
    //         if (res.data.length) {
    //           this.meetingNamevalidator = false
    //           this.meetingNamevalidatorFlag = true
    //           this.$error({
    //             title: '提示',
    //             content: '所属项目该期数已存在，请重新输入',
    //             okText: '确定'
    //           })
    //         } else {
    //           this.meetingNamevalidator = true
    //           this.meetingNamevalidatorFlag = false
    //         }
    //       } catch (error) {
    //         this.meetingNamevalidator = true
    //         this.listing = false
    //       }
    //     }
      
    // },
    getMeetingList: _.debounce( async function() {
      if (this.form.meetingName) {
          this.meetingNamevalidatorFlag = true
          const [err, res] = await getMeetingList({
            pageNo: 1,
            pageSize: 100,
            query: {
              meetingName: `监理例会会议纪要-${this.form.meetingName}期`,
              projectId: this.form.projectId
            }
          })
          try {
            if (res.data.length) {
              this.meetingNamevalidator = false
              this.meetingNamevalidatorFlag = true
              this.$error({
                title: '提示',
                content: '所属项目该期数已存在，请重新输入',
                okText: '确定'
              })
            } else {
              this.meetingNamevalidator = true
              this.meetingNamevalidatorFlag = false
            }
          } catch (error) {
            this.meetingNamevalidator = true
          }
        }
    },300),
    openSelectPersonModal() {
      const list = this.form.meetingEmployeeDTOListData
      this.$refs.SelectPersonWithTree.open(list)
    },
    // 提交表单
    submitForm(e) {
      this.form.meetingEmployeeDTOListData.push(e)
      console.log(this.form.meetingEmployeeDTOListData)
      this.$refs.tableEmployee.refresh(true)
    },
    submitForms(e) {
      e.forEach((item) => {
        this.form.meetingEmployeeDTOListData.push(item)
      })
    },
    handleRemove(index) {
      this.form.meetingEmployeeDTOListData.splice(index, 1)
      this.$refs.tableEmployee.refresh(true)
    },
    // 移除表格选中项
    handleRemoveMore() {
      // 若有选中项
      let that = this
      that.$confirm({
        title: '温馨提示',
        content: '确定移除这些数据？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          console.log(that.rowSelection.selectedRowKeys)
          for (let index = 0; index < that.form.meetingEmployeeDTOListData.length; index++) {
            let item = that.form.meetingEmployeeDTOListData[index]
            console.log(index, item.id)
            if (that.rowSelection.selectedRowKeys.includes(item.id)) {
              that.form.meetingEmployeeDTOListData.splice(index, 1)
              index--
            }
          }
          that.$refs.tableEmployee.clearSelected()
          that.$refs.tableEmployee.refresh(true)
          // console.log(that.rowSelection.selectedRowKeys)
        }
      })
    },
    getProjectInfo(id) {
      let params = {
        projectId: id
      }

      getProjectInfo(params).then((res) => {
        this.projectDeptOrgId = res.data.projectDeptOrgId
        this.form.meetingEmployeeDTOListData = []
        this.$refs.tableEmployee?.refresh(true)
        console.log(res)
      })
    },
    changeProject(e) {
      if (e) {
        this.getConstructionProgram(e) //获取专业分包
        this.getReviewPlan(e) //获取评审方案
        this.getDisclosurePlanReq(e)
        this.getProjectInfo(e)
      }
    },
    changeCommentedPlan(id) {
      this.form.programmer = this.constructionProgram.filter((item) => this.form.commentedPlan == item.id)[0]?.creatorName
      if (this.form.value == 7) {
        this.form.meetingName = this.disclosurePlan.filter((item) => this.form.commentedPlan == item.id)[0].name + '交底会'
      }
    },
    changeReviewPlan(id) {
      this.form.programmer = this.reviewPlan.filter((item) => this.form.commentedPlan == item.id)[0].creatorName
    },
    async getReviewPlan(projectId) {
      const [err, res] = await getReviewPlan({ projectId: projectId, typeId: this.form.value })
      if (!err) {
        this.reviewPlan = res.data
      }
    },
    async getConstructionProgram(projectId) {
      const [err, res] = await getConstructionProgram({ projectId: projectId, typeId: this.form.value })
      if (!err) {
        this.constructionProgram = res.data
      }
    },
    async getDisclosurePlanReq(projectId) {
      const [err, res] = await getDisclosurePlan({ projectId: projectId, typeId: this.form.value })
      if (!err) {
        this.disclosurePlan = res.data
      }
    },
    initTitle() {
      switch (this.form.value) {
        case 3:
          this.$route.meta.title = '新增危险源辨识会议'
          break
        case 2:
          this.$route.meta.title = '新增专业分包方案讲评记录'
          break
        case 1:
          this.$route.meta.title = '新增施组方案评审会议记录'
          break
        default:
          break
      }
    },
    getTime(date, dateString, type, name) {
      this[type][name] = dateString
      console.log(this[type][name])
    },
    async getMeetingType() {
      const [err, res] = await getMeetingType()
      if (!err) {
        this.meetingTypeList = res.data
          .filter((v) => v.id !== '648785280249237505')
          .map((item) => {
            item.value = item.category
            item.label = item.name
            return item
          })
        console.log(this.meetingTypeList)
      }
    },
    delItem(idx) {
      this.form.fileList.splice(idx, 1)
      this.$refs.table.refresh(true)
      console.log(this.form.fileList)
    },
    fileArrysData: function (val) {
      this.form.fileList = val
      //   this.$refs.table.refresh(true)
    },

    onChangeRadio(e) {
      console.log(e)
    },
    onChange(e) {
      console.log(e)
      // this.form.value = e.target.value
    },
    onSelectChange(e) {
      console.log('selectedRowKeys changed: ', e)
    },
    commentsVal: function (val) {
      console.log(val)
      this.form.meetingSummary = val
    },
    handleClose() {
      this.$multiTab.close(this.$route.fullpath)
      //  history.go(-1)
    },
    //基本会议
    validateForm() {
      let flag = false
      this.$refs.ruleForm.validate((valid) => {
        flag = valid
        if(!valid) {
          this.$message.warning("请填写完所有必填项信息")
        }
      })
      return flag
    },
    //施工方案评审会议
    validateFormRule(rule) {
      let flagrule = true
      let flagbasis = false
      let flagrule88 = true

      if (rule == 4 || rule == 6) {
        flagbasis = this.validateForm()
        return flagbasis
      } else {
        flagbasis = this.validateForm()
      }
      if (rule == 9) {
        rule = 8
      }
      let ruleForm = 'ruleForm' + rule
      this.$refs[ruleForm].validate((valid) => {
        flagrule = valid
      })

      this.$refs['ruleForm88'].validate((valid) => {
        flagrule88 = valid
      })
      if (flagrule && flagbasis && flagrule88) {
        return true
      } else {
        this.$message.warning('表单填写不完整')
        return false
      }
    },
    changeValue() {
      for (let i = 1; i < 8; i++) {
        if (i != 4 && i != 6) {
          let ruleForm = 'ruleForm' + i
          console.log(ruleForm)
          this.$refs[ruleForm].resetFields()
        }
      }
    },

    async addMeeting() {
      const rule = this.form.value, flag = true
      if(this.form.isHaveMeetCondition === 1) {
        if (!this.validateFormRule(rule)) return
        if (rule == 8 || rule == 9) {
          if (this.form.meetingSummary == '') {
            this.$message.warning('请填写会议纪要')
            return
          }
        } else {
          if (this.form.meetingEmployeeDTOListData.length == 0) {
            this.$message.warning('请选择参会人员')
            return
          }
        }

        this.form.meetingEmployeeDTOListData.forEach((item) => {
          this.form.meetingEmployeeDTOList.push(item.id)
        })
      }else {
        this.$refs.ruleForm.validate((valid) => {})
        if(!this.form.projectId) {
          return this.$message.warning('请选择所属单位')
        }
        if(!this.form.meetingName) {
          return this.$message.warning('请输入监理会议期数')
        }
        if(!this.form.notMeetReason) {
          return this.$message.warning('请输入未召开会议原因')
        }
      }
      if(!flag) return;
      const params = {
        ...this.form,
        meetingName: `监理例会会议纪要-${this.form.meetingName}期`,
        meetingTypeId: this.meetingTypeList.filter((item) => this.form.value == item.category)[0].id
      }
      this.$confirm({
        title: '提交',
        content: '确定提交？',
        onOk: async () => {
          this.loading = true
          const [err, res] = await addMeeting(params)
          this.loading = false
          if (!err) {
            this.$message.success('新增成功')
            this.handleClose()
          }
        }
      })
    },
  }
}
</script>
<style lang='less' scoped>
@deep: ~'>>>';
.tool-btns {
  display: flex;
  justify-content: flex-end;

  @{deep} .ant-btn + .ant-btn {
    margin-left: 12px;
  }
}

.row {
  display: flex;
  flex-direction: column;
}

.custom-form-upload {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  .label {
    width: 100px;
    padding-right: 10px;
    text-align: right;
  }
}
.ruleForm {
  @{deep} .tip-red {
    .ant-form-item-required::before {
      opacity: 1;
    }
  }
}
</style>
